
if (localStorage.getItem("login") == 'false') {
    console.log(111);
    var acc = localStorage.getItem("user");
    $('#dl').html(acc)
 } 
    let tc=document.getElementById("tc")
    let aa=document.getElementById("dl")
    
    let zc = document.getElementById("zc")
    console.log(zc);
    console.log(localStorage.getItem("login"));
    function name1() {
        if(localStorage.getItem("login")!=false){
            zc.style.display = "none";
            tc.style.display="block";
        }else{
            zc.style.display ="block"
        }
        
    }
    
    name1()
    tc.onclick=function(){
        dl.style.display="none";
        zc.style.display="block";
    }



/* 
更新购物车中商品数量 接口

    接口地址：http://jx.xuzhixiang.top/ap/api/cart-update-num.php
    接口请求方式：get
    接口参数：
         uid  用户id
         pid  商品id
         pnum  要添加的商品数量

    使用方式

         http://jx.xuzhixiang.top/ap/api/cart-update-num.php?uid=1&pid=1&pnum=1

    服务器返回json数据 */

//全选 单个复选框的状态和全选复选框的状态保持一致
function quanXuan() {
    $(document).ready(function () {
        $("#checkAll").click(function () {
            if (this.checked == true) {
                $('input[name="subBox"]').prop("checked", this.checked);
            } else {
                $('input[name="subBox"]').prop("checked", false)
            }
        });
        var $subBox = $('input[name="subBox"]');
        $subBox.click(function () {
            $("#checkAll").prop("checked", $subBox.length == $('input[name="subBox"]:checked').length ? true : false);
        })
    })
}
    $.ajax({
        url: "http://jx.xuzhixiang.top/ap/api/cart-list.php",
        type: "get",
        data: {
            id: localStorage.getItem("id"),
        },
        success: function (data) {
            console.log(data);
            let str = "";
            let spgwc = document.getElementById("spgwc");
            for (let index in data.data) {
                str += `
                <li data-id="${data.data[index].pid}">
                <p><input type="checkbox" name="subBox" class="ck" /></p>
                <p class="spimg"><img src=${data.data[index].pimg} alt="" /></p>
                <p class="spname">${data.data[index].pname}</p>
                <p class="spdj">${data.data[index].pprice}</p>
                <p>
                  <span class="jian">-</span>
                  <input type="text" value="${data.data[index].pnum}" class="shu">
                  <span class="jia">+</span>
                </p>
                <p class="spdz">${data.data[index].pprice * data.data[index].pnum}</p>
                <p class="shanchu">×</p>
              </li>
                    `;
            }
            spgwc.innerHTML = str;
    
            function updateData(i) {
                let pid = li[i].getAttribute("data-id");
                let pnum = shu[i].value;
                $.ajax({
                    url: "http://jx.xuzhixiang.top/ap/api/cart-update-num.php",
                    type: "get",
                    data: {
                        uid: localStorage.getItem("id"),
                        pid,
                        pnum,
                    },
                    success: function (data) {
                        console.log(data);
                    }
                })
            }
            let li = spgwc.children;
            let jia = document.querySelectorAll(".jia");
            let jian = document.querySelectorAll(".jian");
            let shu = document.querySelectorAll(".shu");
            let oCheckAll = document.querySelector("#checkAll");
            let ck = document.querySelectorAll(".ck");
            let shanchu = document.querySelectorAll(".shanchu");
            let spdz = document.querySelectorAll(".spdz");
            let spdj = document.querySelectorAll(".spdj");
            let nums = document.querySelector(".mums");
            let num = document.querySelector(".num");
            let totalPrice = document.querySelector(".totalPrice");
            // let num=0
            for (let i = 0; i < jia.length; i++) {
                nums.innerText = parseInt(nums.innerText) + parseInt(shu[i].value);
                // console.log(nums.innerText);
                // 加
                jia[i].onclick = function () {
                    let chushi = shu[i].value;
                    shu[i].value++
                    updateData(i)
                    // console.log(shu[i].value);
                    // console.log(spdj[i].innerText);
                    spdz[i].innerText = parseInt(shu[i].value) * parseInt(spdj[i].innerText);
    
                    nums.innerText = parseInt(nums.innerText) + 1;
                    if (ck[i].checked) {
                        num.innerText++;
                        totalPrice.innerText= parseInt(totalPrice.innerText) + parseInt(spdj[i].innerText)
                    }
                }
                // 减
                jian[i].onclick = function () {
                    if (shu[i].value<2) {
                    shu[i].value=1
                    } else {
                        shu[i].value--
                        nums.innerText = parseInt(nums.innerText) - 1;
                        if (ck[i].checked) {
                            num.innerText--;
                            totalPrice.innerText = parseInt(totalPrice.innerText) - parseInt(spdj[i].innerText);
                        }
                     }
                    updateData(i)
                    spdz[i].innerText = parseInt(shu[i].value) * parseInt(spdj[i].innerText);
                    // if (ck[i].checked) {
                    //     num.innerText--;
                    // }
                }
                // 输入框
                shu[i].onfocus = function () {
                    let chushi = shu[i].value;
                    shu[i].onblur = function () {
                        if (shu[i].value>1) {
                            if (ck[i].checked) {
                                num.innerText = num.innerText - chushi + parseInt(shu[i].value);
                                totalPrice.innerText= parseInt(totalPrice.innerText) +(parseInt(shu[i].value)-chushi)*parseInt(spdj[i].innerText)
                            }
                        } else {
                            shu[i].value = 1
                            if (ck[i].checked) {
                                num.innerText = num.innerText - chushi + 1;
                                totalPrice.innerText = parseInt(totalPrice.innerText) + (chushi-1) * parseInt(spdj[i].innerText);
                            }
                        }
                        updateData(i)
                        nums.innerText = parseInt(nums.innerText) + parseInt(shu[i].value) - chushi;
    
                        spdz[i].innerText = parseInt(shu[i].value) * parseInt(spdj[i].innerText);
                    }
                    
                }
                // 删除
                shanchu[i].onclick = function () {
                    let pid = li[i].getAttribute("data-id");
                    $.ajax({
                        type: "get",
                        url: "http://jx.xuzhixiang.top/ap/api/cart-delete.php",
                        data: {
                            uid: localStorage.getItem("id"),
                            pid,
                        },
                        success: function (data) {
                            // alert(data.msg)
                            spgwc.removeChild(li[i])
                            quanXuan();
                        }
                    });
                    // if (ck[i].checked) {
                        
                    // }
                    ck[i].checked = false;
                    // quanXuan();
                }
            }

          //单个复选框
            for (let i = 0; i < ck.length; i++) {
              
            ck[i].onclick = () => {
                let count = 0; //保存的是选中的数量
                /* num.innerText = 0;
                totalPrice.innerText = 0; */
              for (let j = 0; j < ck.length; j++) {
                if (ck[j].checked) {
                  count++;
                }
                }
                if (ck[i].checked) {
                    num.innerText = parseInt(num.innerText) + parseInt(shu[i].value);
                    totalPrice.innerText = parseInt(totalPrice.innerText) + parseInt(spdz[i].innerText)
                    ck[i].onclick = function () {
                        if (ck[i].checked==false) {
                            num.innerText = parseInt(num.innerText) - parseInt(shu[i].value);
                            totalPrice.innerText = parseInt(totalPrice.innerText) - parseInt(spdz[i].innerText)
                        } else {
                            num.innerText = parseInt(num.innerText) + parseInt(shu[i].value);
                            totalPrice.innerText = parseInt(totalPrice.innerText) + parseInt(spdz[i].innerText)
                        }
                        quanXuan();
                    }
                } else {
                    num.innerText = parseInt(num.innerText) - parseInt(shu[i].value);
                            totalPrice.innerText = parseInt(totalPrice.innerText) - parseInt(spdz[i].innerText)
                }
    
              //判断选中的数量和总数量是否一致
              if (count == ck.length) {
                oCheckAll.checked = true;
              } else {
                oCheckAll.checked = false;
              }
            //   this.getTotalPrice();
              }
          }    
            //全选 单个复选框的状态和全选复选框的状态保持一致
        oCheckAll.onclick = () => {
            for (let i = 0; i <ck.length; i++) {
                ck[i].checked = oCheckAll.checked;
                num.innerText = parseInt(num.innerText) + parseInt(shu[i].value);
                totalPrice.innerText = parseInt(totalPrice.innerText) + parseInt(spdz[i].innerText)
                
            }
            if (oCheckAll.checked!=true) {
                num.innerText = 0;
                totalPrice.innerText = 0;
                quanXuan();
            }
            // this.getTotalPrice();
        }
            
        }
    })
quanXuan();
//1.移入出现btn
$(".item").hover(function () {
    $(this).find(".item-btn").stop().animate({
        "margin-top": -30,
    }, 200)
}, function () {
    $(this).find(".item-btn").stop().animate({
        "margin-top": 28,
    }, 200)
})
